package cc.wfu.type.exception;

import cc.wfu.type.util.WebUtils;
import com.alibaba.fastjson2.JSON;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)  {

        String json = JSON.toJSONString(new ResponseEntity<>(accessDeniedException.getMessage(), HttpStatus.UNAUTHORIZED));
        WebUtils.renderString(response,json);
    }
}